﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Globalization;

namespace Ogdi.Data.DataLoader
{
    public class TableProcessorParams
    {
        public TableProcessorParams() { }
        public TableProcessorParams(TableEntity data) 
        {
            foreach (var prop in data.GetProperties())
            {
                if (prop.Name.ToLower() == "sourcetimezonename")
                    SourceTimeZoneName = prop.Value.ToString();
                if (prop.Name.ToLower() == "partitionkeypropertyname")
                    PartitionKeyPropertyName = prop.Value.ToString();
                if (prop.Name.ToLower() == "RowKeyPropertyName")
                    RowKeyPropertyName = prop.Value.ToString();
                if (prop.Name.ToLower() == "tablemetadatapartitionkeypropertyname")
                    TableMetadataPartitionKeyPropertyName = prop.Value.ToString();
                if (prop.Name.ToLower() == "tablemetadatarowkeypropertyname")
                    TableMetadataRowKeyPropertyName = prop.Value.ToString();
                if (prop.Name.ToLower() == "entitymetadatapartitionkeypropertyname")
                    EntityMetadataPartitionKeyPropertyName = prop.Value.ToString();
                if (prop.Name.ToLower() == "entitymetadatarowkeypropertyname")
                    EntityMetadataRowKeyPropertyName = prop.Value.ToString();
                if (prop.Name.ToLower() == "entityset")
                    EntitySet = prop.Value.ToString();
                if (prop.Name.ToLower() == "entitykind")
                    EntityKind = prop.Value.ToString();

            }
        }

        TimeZoneInfo _sourceTimeZone;
        string _sourceTimeZoneName;
        public string SourceTimeZoneName 
        {
            get { return _sourceTimeZoneName; }
            set
            {
                _sourceTimeZoneName = value;
                if (String.IsNullOrEmpty(_sourceTimeZoneName))
                    _sourceTimeZone = null;
                var timeZones = TimeZoneInfo.GetSystemTimeZones();
                foreach (var zone in timeZones)
                {
                    if (zone.DisplayName == _sourceTimeZoneName)
                    {
                        _sourceTimeZone = zone;
                        return;
                    }
                }
                //if timezone not found, throw exception.
                throw new TimeZoneNotFoundException(_sourceTimeZoneName);
            }
        }
        public TimeZoneInfo getSourceTimeZone()
        {
            return _sourceTimeZone;
        }

        public string PartitionKeyPropertyName { get; set; }
        
        public string RowKeyPropertyName { get; set; }
        
        public string TableMetadataPartitionKeyPropertyName { get; set; }
        
        public string TableMetadataRowKeyPropertyName { get; set; }
        
        public string EntityMetadataPartitionKeyPropertyName { get; set; }
        
        public string EntityMetadataRowKeyPropertyName { get; set; }

        public string EntitySet { get; set; }

        public string EntityKind { get; set; }

        public static implicit operator Entity(TableProcessorParams me)
        {
            Entity e = new Entity();
            foreach (var pi in me.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public))
            {
                e.AddProperty(pi.Name, pi.GetValue(me, null));
            }
            return e;
        }
    }
}
